<?php
function upload($file,$path='xiazai',$allowType = array('png','gif','jpg'),$maxSize = 200000000){
	$name=$file['name'];
	$type=$file['type'];
	$tmp_name=$file['tmp_name'];
	$error=$file['error'];
	$size=$file['size'];
	
	if($error == 0){
		if($size>$maxSize){
			die('上传文件过大');
		}
		$ext = pathinfo($name)['extension'];
		if(!in_array($ext, $allowType)){
			die('上传文件格式不正确');
		}
		if(!is_uploaded_file($tmp_name)){
			die('非http post上传');
		}
		$uniqueName = md5(time()).'.'.$ext;
		$destination=$path.'/'.$uniqueName;
		if(!file_exists($path)){
			mkdir($path,0777,true);
			chmod($path,0777);
		}
		if(@!getimagesize($tmp_name)){
			die('不是真正的图片类型');
		}
		if(@move_uploaded_file($tmp_name,$destination)){
			return $uniqueName;
		}else{
			echo '上传失败';
		}
	}else{
		switch ($error) {
			case 1:
				die('上传文件超过了php.ini中upload_max_filesize玄子昂的值');
				break;
			case 2:
				die('上传文件的大小超过了html表单中MAX_FILE_SIZE指定的值');
				break;
			case 3:
				die('文件只有部分被上传');
				break;
			case 4:
				die('没有文件被上传');
				break;
			case 6:
				die('找不到临时文件夹');
				break;
			case 7:
				die('文件写入失败');
				break;
			case 8:
				die('上传被php拓展程序中断');
				break;
		}
	}
}
